package com.tenglong.teacher.ui.answerq.answer;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cuotibao.zhuoxiao.basics.BaseFragment;
import com.tenglong.teacher.R;
import com.tenglong.teacher.ui.port.MainFragmentRubsh;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyAnswerFragment extends BaseFragment implements View.OnClickListener, MainFragmentRubsh {

    public static final String NOT_ANSWER = "not_answer";
    public static final String ALREADY_ANSWER = "already_answer";
    public static final String ALREADY_COLLECT = "already_collect";

    private static final String KEY_MY_ANSWER_POSITIPN = "key_my_answer_position";

    private String[] answerTags;
    private ViewGroup navigation;
    private int mLastTabPosition = -1;
    private boolean isLoadWindow = false;

    public MyAnswerFragment() {
        // Required empty public constructor
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        answerTags = new String[]{NOT_ANSWER, ALREADY_ANSWER, ALREADY_COLLECT};
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my_answer, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (savedInstanceState != null && savedInstanceState.containsKey(KEY_MY_ANSWER_POSITIPN)) {
            mLastTabPosition = savedInstanceState.getInt(KEY_MY_ANSWER_POSITIPN);
        }
        initView(view);
        setListeners();
        onClick(navigation.getChildAt(mLastTabPosition==-1?0:mLastTabPosition));
        isLoadWindow = true;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_MY_ANSWER_POSITIPN, mLastTabPosition);
    }

    private void setListeners() {
        for (int i = 0; i < navigation.getChildCount(); i++) {
            View childView = navigation.getChildAt(i);
            childView.setId(i);
            childView.setOnClickListener(this);
        }
    }

    private void initView(View view) {
        navigation = view.findViewById(R.id.navigation);
    }

    private Fragment getFragment(String tag) {
        FragmentManager manager = getChildFragmentManager();
        Fragment fragment = manager.findFragmentByTag(tag);
        return fragment == null ? MyAnswerChildFragment.getInstance(tag) : fragment;
    }

    private void setFragmentToRes(String tag, String hideTag) {
        if (hideTag != null && tag.equals(hideTag)) return;
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment showFragment = manager.findFragmentByTag(tag);
        Fragment hideFragment = manager.findFragmentByTag(hideTag);
        if (showFragment == null) {
            transaction.add(R.id.child_anserques_container, getFragment(tag), tag);
        } else {
            if (showFragment.isHidden()) {
                transaction.show(showFragment);
            }
        }
        if (hideFragment != null) {
            if (!hideFragment.isHidden()) {
                transaction.hide(hideFragment);
            }
        }
        transaction.commit();
    }


    private void setNavigation(int index) {
        if (mLastTabPosition == index && (((ViewGroup) navigation.getChildAt(0)).getChildAt(0).isSelected()
                || ((ViewGroup) navigation.getChildAt(1)).getChildAt(0).isSelected()
                || ((ViewGroup) navigation.getChildAt(2)).getChildAt(0).isSelected())) return;
        ViewGroup group = (ViewGroup) navigation.getChildAt(index);
        group.getChildAt(0).setSelected(true);
        group.getChildAt(1).setBackgroundColor(ContextCompat.getColor(mContext, R.color.theme_green));
        if (mLastTabPosition != -1 && mLastTabPosition != index) {
            ViewGroup group2 = (ViewGroup) navigation.getChildAt(mLastTabPosition);
            group2.getChildAt(0).setSelected(false);
            group2.getChildAt(1).setBackgroundColor(Color.parseColor("#ffffff"));
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id >= 0 && id <= 3) {
            if (mLastTabPosition == -1) {
                setFragmentToRes(answerTags[id], null);
            } else {
                setFragmentToRes(answerTags[id], answerTags[mLastTabPosition]);
            }
            setNavigation(id);
            if (!isHidden() && mLastTabPosition != id) {
                rubsh(id);
            }
            mLastTabPosition = id;
        }
    }

    public boolean isFragmentExit(String tag) {
        FragmentManager manager = getChildFragmentManager();
        Fragment f = manager.findFragmentByTag(tag);
        return f != null;
    }

    @Override
    public void refresh() {
        rubsh(mLastTabPosition);
    }

    private void rubsh(int index) {
        if (index == -1) return;
        String tag = answerTags[index];
        if (isAdded() && isLoadWindow && isFragmentExit(tag)) {
            Fragment f = getFragment(tag);
            if (f instanceof MainFragmentRubsh) {
                ((MainFragmentRubsh) f).refresh();
            }
        }
    }
}
